package com.zl.tby.screen.vo.education;

import com.zl.tby.core.base.util.ToolsUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 活动统计
 * @author changpeng
 * @version 1.0
 * @since 2024/8/16 12:33
 */
@Data
public class TbyActivitiesStatisticBean {
    private Long typeId;
    private String typeCode;
    private String typeName;
    private Long total;
    private Long value;
    private Double percentage;

    public void cal() {
        if (total.compareTo(0L) == 0 || value.compareTo(0L) == 0) {
            this.percentage = BigDecimal.ZERO.doubleValue();
            return;
        }
        BigDecimal temp = ToolsUtil.divide(new BigDecimal(this.value), new BigDecimal(this.total), 7);
        temp = temp.multiply(new BigDecimal(100));
        temp = temp.setScale(2, BigDecimal.ROUND_HALF_UP);
        this.percentage = (double) Math.round(temp.doubleValue());
    }

    public static void resetPercentage(List<TbyActivitiesStatisticBean> dbList) {
        BigDecimal full = new BigDecimal(100);
        // 验证全部大于0
        for (TbyActivitiesStatisticBean data : dbList) {
            if (data.getPercentage().doubleValue() > 0) {
                return;
            }
        }
        // 避免多次四舍五入不满100的情况
        BigDecimal result = BigDecimal.ZERO;
        if (dbList.size() > 1) {
            for (int i = 0; i < dbList.size() - 1; i++) {
                result = result.add(new BigDecimal(dbList.get(i).getPercentage()));
            }
            dbList.get(dbList.size() - 1).setPercentage(full.subtract(result).doubleValue());
        }
    }
}
